import unittest
import requests
import sys
import os
from time import sleep

from parameterized import parameterized
# 加入项目根路径
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
from tools import read_json
from api import log
from tools import parser_html
from api.api_approve_trust import ApiApproveTrust
from api.api_register_login import ApiRegisterLogin
class Test02ApproveTrust(unittest.TestCase):

    def setUp(self) -> None: #unittest中初始化用setup
        self.session = requests.session()
        self.approve = ApiApproveTrust(self.session)
        ApiRegisterLogin(self.session).api_login() #直接登录
    def tearDown(self) -> None:
        self.session.close()

    #认证接口测试
    def test01_approve(self):
        try:
            r = self.approve.api_approve()
            self.assertIn("提交成功", r.text)
            log.info("认证接口执行结果为:{}".format(r.text))
            log.info("认证接口测试通过")
        except Exception as e:
            log.error("断言错误:{}".format(e))
 
    #查询认证接口测试 
    def test02_approve_status(self):
        try:
            r = self.approve.api_approve_status()
            self.assertIn("张", r.text)
            log.info("查询认证接口执行结果为:{}".format(r.text))
            log.info("查询认证接口测试通过")
        except Exception as e:
            log.error("断言错误:{}".format(e))
            raise
    #开户接口测试
    def test03_trust(self):
        try:
            r = self.approve.api_trust()
            self.assertIn("form", r.text)
            log.info("开户接口执行结果为:{}".format(r.text))
            #print("开户接口结果：{}",format(r.json()))
            log.info("开户接口测试通过")  
            #三方开户
            result =  parser_html(r)
            r = self.session.post(url=result[0], data=result[1]) 
            print("三方开户接口结果：",r.text)
            self.assertIn("OK", r.text)
        except Exception as e:
            log.error("断言错误:{}".format(e))
            raise
    #图片验证码测试
    @parameterized.expand(read_json("approve_trust.json","img_code"))
    def test04_img_code(self, random, except_code):
        try:
            #1.调用图片验证码接口
            r = self.approve.api_img_code(random)
            log.info("获取图片验证码状态码:{}".format(r.status_code))
            #2.查看状态码
            self.assertEqual(r.status_code, except_code)
            log.info("执行图片验证码断言通过")
        except Exception as e:
            #查看日志
            log.error("获取图片验证码失败:{}".format(e))
            #抛出异常
            raise
    #充值接口测试
    @parameterized.expand(read_json("approve_trust.json","recharge"))
    def test05_recharge(self, valicode, except_text):
        try:
            #调用短信验证码
            self.approve.api_img_code(666666)
            #1.调用图片验证码接口
            r = self.approve.api_recharge(valicode)
            log.info("获取充值接口响应结果:{}".format(r.json()))
            if valicode == 8888:
                #2.查看状态码
                self.assertIn("form",r.text)
                log.info("执行充值断言通过")
                #3.三方充值
                result =  parser_html(r)
                r = self.session.post(url=result[0], data=result[1]) 
                print("三方充值接口结果：",r.text)
                self.assertIn(except_text, r.text)
            else:
                 self.assertIn("100", r.text)
        except Exception as e:
            #查看日志
            log.error("充值失败:{}".format(e))
            #抛出异常
            raise

if __name__ == '__main__':
    unittest.main()